<?PHP
include_once "ini.php";
include "url.php";
dbconnect();

$position = $_POST['position'];

$coord = split(',', $position);
$distance = 50;
$xmin = ((int)$coord[0])-$distance;
$xmax = ((int)$coord[0])+$distance;
$zmin = ((int)$coord[2])-$distance;
$zmax = ((int)$coord[2])+$distance;
$sql = "SELECT * FROM images WHERE coord_x < '".$xmax."' AND coord_x > '".$xmin."' AND coord_z < '".$zmax."' AND coord_z > '".$zmin."' AND droit = 'public' AND posted = 'oui';";
$req = mysql_query($sql);
echo $sql, "\r\n";
if (!$req)
{
    echo "SQL error : ", $sql;
    exit ;
}
else
{
    $file = "../imagePublic.txt";
    $fp = fopen($file, "w");

    while ($tab = mysql_fetch_assoc($req))
    {
        $ligne = $tab['coord_x'].",".$tab['coord_y'].",".$tab['coord_z']."	".$tab['orientation_dir']."	".$tab['orientation_up']."	".$tab['nom_image']."	".$url."images/".$tab['nom_image']."	".$tab['id_image']."\n";
        fputs($fp, $ligne);
    }
    fclose($fp);
}


$sql = "SELECT * FROM images WHERE coord_x < '".$xmax."' AND coord_x > '".$xmin."' AND coord_z < '".$zmax."' AND coord_z > '".$zmin."' AND droit = 'private' AND posted = 'oui';";
$req = mysql_query($sql);
echo $sql, "\r\n";
if (!$req)
{
    echo "SQL error : ", $sql;
    exit ;
}
else
{
    $file = "../imagePrive.txt";
    $fp = fopen($file, "w");

    while ($tab = mysql_fetch_assoc($req))
    {
        $ligne = $tab['coord_x'].",".$tab['coord_y'].",".$tab['coord_z']."	".$tab['orientation_dir']."	".$tab['orientation_up']."	".$tab['nom_image']."	".$url."images/".$tab['nom_image']."	".$tab['id_image']."\n";
        fputs($fp, $ligne);
    }
    fclose($fp);
}
dbclose();

?>
